﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Collections;

public partial class clientes : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            SqlConnection conexion = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            conexion.Open();
            string usuario = txt_usuario.Text;
            string consulta = "Select count(*) from funcionario where usuario = '" + usuario + "'";
            SqlCommand existente = new SqlCommand(consulta, conexion);
            int temp = Convert.ToInt32(existente.ExecuteScalar().ToString());
            conexion.Close();
                
            if (temp > 0)
                {
                    Response.Redirect("error.aspx");                
                }
        }
    }
    


    protected void imgbt_guardarPersona_Click(object sender, ImageClickEventArgs e)
    {
        String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conexion = new SqlConnection(cadena);
        conexion.Open();
        try
        {
            if (txt_nombre.Text != "" && txt_CI.Text != "")
            {

                SqlCommand comando = new SqlCommand();
                comando.Connection = conexion;
                comando.CommandType = System.Data.CommandType.StoredProcedure;
                comando.CommandText = "insertar_persona";
                comando.Parameters.AddWithValue("@CI", txt_CI.Text);
                comando.Parameters.AddWithValue("@nombre", txt_nombre.Text);
                comando.Parameters.AddWithValue("@apellido", txt_apellido.Text);
                comando.Parameters.AddWithValue("@direccion", txt_direccion.Text);
                comando.Parameters.AddWithValue("@telefono", txt_telefono.Text);
                comando.Parameters.AddWithValue("@email", txt_email.Text);
                SqlDataReader reader = comando.ExecuteReader();
                lb_Estado.Text = "Activo";
                dl_persona.DataBind();
                dl_persona2.DataBind();
                dl_persona3.DataBind();
                txt_CI.Text = "";
                txt_nombre.Text = "";
                txt_apellido.Text = "";
                txt_direccion.Text = "";
                txt_telefono.Text = "";
                txt_email.Text = "";
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta (' Se creo una nueva persona') </SCRIPT>");
            }
            else
            {
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta ('debe completar los campos correspondientes') </SCRIPT>");
            }

           
        }
        catch (Exception error)
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");

        }
        finally
        {
            conexion.Close();
            
        }
    }

   

    protected void imgbt_guardarCliente_Click1(object sender, ImageClickEventArgs e)
    {
        String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conexion = new SqlConnection(cadena);
        conexion.Open();
        try
        {

            SqlCommand comando = new SqlCommand();
            comando.Connection = conexion;
            comando.CommandType = System.Data.CommandType.StoredProcedure;
            comando.CommandText = "insertar_cliente";
            comando.Parameters.AddWithValue("@cod_tipo_cliente",  dl_tipoCliente.Text);
            comando.Parameters.AddWithValue("@cod_persona", dl_persona.Text);
            SqlDataReader reader = comando.ExecuteReader();
            lb_Estado2.Text = "Activo";
            dl_tipoCliente.Text = "";
            dl_persona.Text = "";
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta (' Se creo un nuevo cliente') </SCRIPT>");
            dl_persona.DataBind();
        }
        catch (Exception error)
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");

        }
        finally
        {
            conexion.Close();
            
        }
    }
    protected void imgbt_guardarFuncionario_Click(object sender, ImageClickEventArgs e)
    {
        if (txt_usuario.Text == "" || txt_clave.Text == "")
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta (' Los campos no deben estar vacios') </SCRIPT>");
        }
        else
        {
            String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection conexion = new SqlConnection(cadena);
            conexion.Open();
            try
            {

                SqlCommand comando = new SqlCommand();
                comando.Connection = conexion;
                comando.CommandType = System.Data.CommandType.StoredProcedure;
                comando.CommandText = "insertar_funcionario";
                comando.Parameters.AddWithValue("@cod_tipo_funcionario", dl_tipoFuncionario.Text);
                comando.Parameters.AddWithValue("@cod_persona", dl_persona2.Text);
                comando.Parameters.AddWithValue("@usuario", txt_usuario.Text);
                comando.Parameters.AddWithValue("@clave", txt_clave.Text);
                comando.Parameters.AddWithValue("@tipo_usuario", dl_tipo_usuario.Text);
                SqlDataReader reader = comando.ExecuteReader();
                lb_Estado3.Text = "Activo";
                dl_tipoFuncionario.Text = "";
                dl_persona2.Text = "";
                dl_persona2.DataBind();

            }
            catch (Exception error)
            {
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta (' Se creo un nuevo funcionario') </SCRIPT>");
            }
            finally
            {
                conexion.Close();
            }
        }
    }
    protected void imgbt_guardarProveedor_Click(object sender, ImageClickEventArgs e)
    {
        String cadena = (String)ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conexion = new SqlConnection(cadena);
        conexion.Open();

        if (txt_personaContacto.Text == "")
        {
            Page.RegisterStartupScript("alerta", "<SCRIPT> alerta ('Debe completar el campo Persona de Contacto') </SCRIPT>");
        }
        else
        {
            try
            {

                SqlCommand comando = new SqlCommand();
                comando.Connection = conexion;
                comando.CommandType = System.Data.CommandType.StoredProcedure;
                comando.CommandText = "insertar_proveedor";
                comando.Parameters.AddWithValue("@persona_de_contacto", txt_personaContacto.Text);
                comando.Parameters.AddWithValue("@cod_persona", dl_persona3.Text);
                SqlDataReader reader = comando.ExecuteReader();
                lb_Estado4.Text = "Activo";
                txt_personaContacto.Text = "";
                dl_persona3.Text = "";
                dl_persona3.DataBind();
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta (' Se creo un nuevo proveedor') </SCRIPT>");
            }
            catch (Exception error)
            {
                Page.RegisterStartupScript("alerta", "<SCRIPT> alerta(" + error.Message.ToString() + ") </SCRIPT>");

            }
            finally
            {
                conexion.Close();
                
            }
        }
    }
    protected void imgbt_persona_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void img_cancelar_Click(object sender, ImageClickEventArgs e)
    {
        txt_CI.Text = "";
        txt_nombre.Text = "";
        txt_apellido.Text = "";
        txt_direccion.Text = "";
        txt_telefono.Text = "";
        txt_email.Text = "";
    }

    protected void bt_personaListar_Click(object sender, EventArgs e)
    {
        GridPersonaListar.DataBind();
       // GridPersonaListar.Visible = true;
        
    }
    protected void bt_listarClientes_Click(object sender, EventArgs e)
    {
        GridClienteListar.DataBind();
        //GridClienteListar.Visible = true;
    }

    protected void imgbt_cancelar3_Click(object sender, ImageClickEventArgs e)
    {
        txt_clave.Text = "";
        txt_usuario.Text = "";
    }

    protected void bt_listarFuncionarios_Click(object sender, EventArgs e)
    {
        GridFuncionariosListar.DataBind();
        //GridFuncionariosListar.Visible = true;
    }
    protected void imgbt_cancelar4_Click(object sender, ImageClickEventArgs e)
    {
        txt_personaContacto.Text = "";

    }

 
}
